package back_end;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Api {
    private Router o;
    protected MysqlConnection mc;
    public Api() {
        try {
			mc = new MysqlConnection();
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
        o = new Router();
    }
    //鏌ヨ鏌愪釜鎿嶄綔瑕佺敤鍝簺鍙傛暟
    public String operate(String s) {
        if (!o.hm.containsKey(s)) {
            return "Fail: operation not found";
        }
        StringBuilder ret = new StringBuilder();
        for (String f : o.hm.get(s).needs) {
            ret.append(" ").append(f);
        }
        return "Success" + ret;
    }
    public String operate(String s1, String s2) {
        if (!o.hm.containsKey(s1)) {
            return "Fail: operation not found";
        }
        o.hm.get(s1).init(s2);
        return o.hm.get(s1).perform();
    }
    public String SignIn(String num, String pwd) {
        Connection conn = MysqlConnection.getConn();
        Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet rst = stmt.executeQuery("select * from students where number=" + num + ";");
	        if (rst.next()) {
	            if (rst.getString("password").equals(pwd)) {
	                return "Success " + o.show(rst.getInt("permission"));
	            } else {
	                return "Fail: password error!";
	            }
	        } else {
	            return "Fail: unknown user";
	        }
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return "False";
    }
    public String SignUp(String nums, String pwd, String name) {
        Connection conn = MysqlConnection.getConn();
        int q = 0;
        Long num = Long.parseLong(nums);
        PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement("insert into students (number, password, name, permission, count) values(?, ?, ?, ?, ?);");
			pstmt.setLong(1, num);
	        pstmt.setString(2, pwd);
	        pstmt.setString(3, name);
	        pstmt.setInt(4, 1);
	        pstmt.setInt(5, 0);
	        q = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			
		}
        //int q = stmt.executeUpdate("insert into students (number, password, name, permission, count) values();");
        //int q = stmt.executeUpdate("insert into students values() number=" + num + ", password=" + pwd + ", permission=1, count=0;");
        
        if (q == 1) {
            return "Success";
        } else {
            return "Fail.";
        }
    }
    public static void main(String[] args) throws Exception {
        Api a = new Api();
//        System.out.println(a.operate("search"));
//        System.out.println(a.operate("search", "鑰�"));
//        System.out.println(a.operate("showBorrowed", "12345"));
//        System.out.println(a.operate("reeturn", "12345 17"));
        a.o.show(1);
    }
}
